package com.zangchat.controller;

import com.common.core.domain.AjaxResult;
import com.zangchat.service.CommonFileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;

/**
 * 通用文件上传下载控制层
 * @author liuduan
 */
@CrossOrigin
@RestController
@RequestMapping("/zangchat/files")
public class CommonFileController {

    @Autowired
    private CommonFileService commonFileService;

    /**
     * 上传文件
     * @param files 文件对象
     * @param type 上传图片的作用类型
     * @return
     */
    @RequestMapping("/putFiles")
    public AjaxResult putFiles(@ModelAttribute("files") MultipartFile [] files, @ModelAttribute("type") String type){
        return commonFileService.putFiles(files,type);
    }






    public static void main(String[] args) {
        try {
            URL url=new URL("https://dirty-1257961131.cos.ap-beijing.myqcloud.com/zangchat/idImg/timg.jpg");
            InputStream inputStream = url.openStream();
//            File picture = new File("https://dirty-1257961131.cos.ap-beijing.myqcloud.com/zangchat/idImg/timg.jpg");
            BufferedImage sourceImg = null;
            sourceImg = ImageIO.read(inputStream);
//            System.out.println(String.format("Size: %.1f KB", picture.length()/1024.0));
            System.out.println("Width: " + sourceImg.getWidth());
            System.out.println("Height: " + sourceImg.getHeight());
        } catch (IOException e) {
            e.printStackTrace();
        }

    }





}
